home *** CD-ROM | disk | FTP | other *** search
- /*
- IC Dialogs.c
-
- Code to handle dialogs.
-
- */
-
- #include <TextUtils.h>
-
- #include "IC Misc Subs.h"
-
- #include "IC Dialogs.h"
-
- void SetItemText(DialogPtr dlg,short item,const StringPtr text){
- short it;
- Handle ih;
- Rect box;
- Str255 oldtext;
-
- GetDialogItem(dlg,item,&it,&ih,&box);
- GetDialogItemText(ih,oldtext);
-
- if (IUEqualString(oldtext,text)!=0)
- SetDialogItemText(ih,text);
- }
-
- void GetItemText(DialogPtr dlg,short item,StringPtr text){
- short it;
- Handle ih;
- Rect box;
-
- GetDialogItem(dlg,item,&it,&ih,&box);
- GetDialogItemText(ih,text);
- }
-
- StringPtr GetItemTextF(DialogPtr dlg,short item,StringPtr text){
- GetItemText(dlg,item,text);
- return text;
- }
-
- pascal void OutlineDefault1(DialogPtr dp,short item){
- short kind;
- Handle h;
- Rect r;
-
- SetPort(dp);
- GetDialogItem(dp,1,&kind,&h,&r);
- PenSize(3,3);
- InsetRect(&r,-4,-4);
-
- if ((*((ControlHandle)h))->contrlHilite==255)
- PenPat(&qd.gray);
- FrameRoundRect(&r,16,16);
- if ((*((ControlHandle)h))->contrlHilite==255)
- PenPat(&qd.black);
- PenNormal();
- }
-
- void SetUpDefaultOutline(DialogPtr dp,short def_item,short user_item){
- short kind;
- Handle h;
- Rect r;
-
- if (def_item!=1)
- return; // can't handle anything except 1 yet
-
- GetDialogItem(dp,user_item,&kind,&h,&r);
-
- InsetRect(&r,-10,-10);
-
- kind=userItem;
-
- SetDialogItem(dp,user_item,kind,(Handle)gOutlineDefault1,&r);
- }
-
- void FlashItem(DialogPtr dlg,short item){
- short kind;
- Handle h;
- Rect r;
- long f;
-
- GetDialogItem(dlg,item,&kind,&h,&r);
- // HiliteControl((ControlHandle)h,kControlButtonPart);
- HiliteControl((ControlHandle)h,inButton);
- Delay(2,&f);
- HiliteControl((ControlHandle)h,0);
- }
-
- void SetDItemRect(DialogPtr dp,short item,Rect* rr){
- short kind;
- Handle h;
- Rect r;
-
- GetDialogItem(dp,item,&kind,&h,&r);
- SetDialogItem(dp,item,kind,h,rr);
- }
-
- void GetDItemRect(DialogPtr dp,short item,Rect* rr){
- short kind;
- Handle h;
-
- GetDialogItem(dp,item,&kind,&h,rr);
- }
-
- void SetDItemKind(DialogPtr dp,short item,short k){
- short kind;
- Handle h;
- Rect r;
-
- GetDialogItem(dp,item,&kind,&h,&r);
- SetDialogItem(dp,item,k,h,&r);
- }
-
- void GetDItemKind(DialogPtr dp,short item,short* k){
- Rect r;
- Handle h;
-
- GetDialogItem(dp,item,k,&h,&r);
- }
-
- ControlHandle GetDControlHandle(DialogPtr dp,short item){
-
- return (ControlHandle)GetDItemHandle(dp,item);
- }
-
- Handle GetDItemHandle(DialogPtr dp,short item){
- short kind;
- Handle h;
- Rect r;
-
- GetDialogItem(dp,item,&kind,&h,&r);
- return h;
- }
-
- void SetDItemHandle(DialogPtr dp,short item,Handle h){
- short kind;
- Handle hh;
- Rect r;
-
- GetDialogItem(dp,item,&kind,&hh,&r);
- SetDialogItem(dp,item,kind,h,&r);
- }
-
- Boolean GetDCtlEnable(DialogPtr dp,short item){
- short k;
- Handle h;
- Rect r;
-
- GetDialogItem(dp,item,&k,&h,&r);
-
- return (*((ControlHandle)h))->contrlHilite!=255;
- }
-
- void SetDCtlEnable(DialogPtr dp,short item,Boolean on){
- ControlHandle ch;
- short hilite;
-
- ch=GetDControlHandle(dp,item);
- hilite=on?0:255;
-
- if ((*ch)->contrlHilite!=hilite)
- HiliteControl(ch,hilite);
- }
-
- StringPtr GetDCtlTitle(DialogPtr dp,short item,StringPtr str){
- GetControlTitle(GetDControlHandle(dp,item),str);
- return str;
- }
-
- void SetDCtlTitle(DialogPtr dp,short item,StringPtr s){
- ControlHandle ch=GetDControlHandle(dp,item);
- Str255 old;
-
- GetControlTitle(ch,old);
-
- if (IUEqualString(s,old)!=0)
- SetControlTitle(ch,s);
- }
-
- Boolean GetDCtlBoolean(DialogPtr dp,short item){
- return (Boolean)GetControlValue(GetDControlHandle(dp,item))!=0;
- }
-
- void SetDCtlBoolean(DialogPtr dp,short item,Boolean value){
- short v=value?1:0;
-
- SetControlValue(GetDControlHandle(dp,item),v);
- }
-
- void ToggleDCtlBoolean(DialogPtr dp,short item){
- SetDCtlBoolean(dp,item,!GetDCtlBoolean(dp,item));
- }
-
- short GetDCtlValue(DialogPtr dp,short item){
- return GetControlValue(GetDControlHandle(dp,item));
- }
-
- void SetDCtlValue(DialogPtr dp,short item,short value){
-
- SetControlValue(GetDControlHandle(dp,item),value);
- }
-
- void DrawDItem(DialogPtr dp,short item){
- Draw1Control(GetDControlHandle(dp,item));
- }
-
- MenuHandle GetPopupMHandle(DialogPtr dp,short item){
- typedef MenuHandle* MenuHandlePtr,** MenuHandleHandle;
- MenuHandleHandle mhh;
- ControlHandle ch=GetDControlHandle(dp,item);
-
- mhh=(MenuHandleHandle)(*ch)->contrlData;
-
- return **mhh;
- }
-
- void SetPopUpMenuOnMouseDown(DialogPtr dlg,short item,StringPtr text){
- MenuHandle mh=GetPopupMHandle(dlg,item);
- short i,index=0;
- Str255 s;
-
- if (text[0]==0)
- GetMenuItemText(mh,1,text);
-
- GetMenuItemText(mh,2,s);
-
- if (IUEqualString(s,"\p-")==0){
- DeleteMenuItem(mh,2);
- DeleteMenuItem(mh,1);
- }
-
- index=0;
-
- for (i=1;i<=CountMItems(mh);i++){
- GetMenuItemText(mh,i,s);
- if (IUEqualString(s,text)==0){
- index=i;
- break;
- }
- }
-
- if (index==0){
- InsertMenuItem(mh,"\p(-;fred",0);
- SetMenuItemText(mh,1,text);
- index=1;
- }
-
- SetDCtlValue(dlg,item,index);
- }
-
- void GetPopUpItemText(DialogPtr dlg,short item,StringPtr text){
- MenuHandle mh=GetPopupMHandle(dlg,item);
-
- GetMenuItemText(mh,GetDCtlValue(dlg,item),text);
- }
-
- void GetDAFont(short* font){
-
- *font=LMGetDlgFont();
- }
-
- void SetWindowTitle(WindowPtr window,const StringPtr title){
- Str255 s;
-
- GetWTitle(window,s);
-
- if (IUEqualString(s,title))
- SetWTitle(window,title);
- }
-
- short SelectedTextItem(DialogPtr dlg){
- DialogPeek dp=(DialogPeek)dlg;
-
- return dp->editField+1;
- }
-
- short CountDItems(DialogPtr dlg){
- typedef short* IntegerPtr,** IntegerHandle;
-
- DialogPeek dp=(DialogPeek)dlg;
- IntegerHandle ih=(IntegerHandle)dp->items;
-
- return (**ih)+1;
- }
-
- void ShiftTab(DialogPtr dlg){
- short orgitem,i,count,k;
-
- orgitem=SelectedTextItem(dlg);
- count=CountDItems(dlg);
-
- if ((orgitem>0)&&(count>1)){
- i=orgitem-1;
- do {
- if (i==0)
- i=count;
- GetDItemKind(dlg,i,&k);
- i--;
- } while ((i!=orgitem)&&(k!=editText));
- }
-
- GetDItemKind(dlg,i,&k);
-
- if (k==editText)
- SelectDialogItemText(dlg,i,0,255);
- }
-
- void DrawTheFriggingGrowIcon(WindowPtr wind,Rect* bounds){
- RgnHandle clip;
-
- SetPort(wind);
- PenNormal();
- clip=NewRgn();
- GetClip(clip);
- ClipRect(bounds);
- DrawGrowIcon(wind);
- SetClip(clip);
- DisposeRgn(clip);
- }
-
- Boolean DoButtonKey(DialogPtr dlg,short item,EventRecord* er,short* result_item){
-
- if (GetDCtlEnable(dlg,item)){
- *result_item=item;
- FlashItem(dlg,item);
- return true;
- }
-
- SysBeep(10);
- er->what=nullEvent;
- return false;
- }
-
- pascal Boolean OKModalFilter(DialogPtr dlg,EventRecord* er,short* item){
- char ch;
-
- if ((er->what==keyDown)||(er->what==autoKey)){
- ch=er->message&0x00ff;
-
- if ((ch==13)||(ch==3))
- return DoButtonKey(dlg,i_ok,er,item);
- }
-
- return false;
- }
-
- pascal Boolean CancelModalFilter(DialogPtr dlg,EventRecord* er,short* item){
- char ch;
-
- if ((er->what==keyDown)||(er->what==autoKey)){
- ch=er->message&0x00ff;
-
- if ((ch==13)||(ch==3))
- return DoButtonKey(dlg,i_ok,er,item);
- else if (((ch=='.')&&(er->modifiers&cmdKey))||(ch==27))
- return DoButtonKey(dlg,i_cancel,er,item);
- }
-
- return false;
- }
-
- pascal Boolean CancelDiscardModalFilter(DialogPtr dlg,EventRecord* er,short* item){
- char ch;
-
- if (CancelModalFilter(dlg,er,item))
- return true;
-
- if ((er->what==keyDown)||(er->what==autoKey)){
- ch=er->message&0x00ff;
-
- if ((ch=='d')&&(er->modifiers&cmdKey))
- return DoButtonKey(dlg,i_discard,er,item);
- }
-
- return false;
- }
-
- void EnterWindow(WindowPtr window,short font,short size,Style face,SavedWindowInfo* saved){
-
- GetPort(&(saved->oldport));
- SetPort(window);
-
- saved->thisport=window;
- saved->font=window->txFont;
- saved->size=window->txSize;
- saved->face=window->txFace;
-
- TextFont(font);
- TextSize(size);
- TextFace(face);
- }
-
- void ExitWindow(SavedWindowInfo* saved){
-
- SetPort(saved->thisport);
- TextFont(saved->font);
- TextSize(saved->size);
- TextFace(saved->face);
-
- SetPort(saved->oldport);
- }
-
- void DrawGrayRect(DialogPtr dlg,short item,StringPtr title){
- #define left_indent 20
- #define gap 2
-
- Rect r,er;
- FontInfo fi;
- short sw;
-
- GetDItemRect(dlg,item,&r);
- GetFontInfo(&fi);
-
- MoveTo(r.left+left_indent,r.top+fi.ascent);
- sw=StringWidth(title);
-
- er.top=r.top;
- er.bottom=er.top+fi.ascent+fi.descent;
- er.left=r.left+left_indent;
- er.right=er.left+sw;
-
- EraseRect(&er);
-
- DrawString(title);
-
- PenPat(&qd.gray);
- r.top += (fi.ascent/2);
-
- MoveTo(er.left-gap,r.top);
-
- LineTo(r.left,r.top);
- LineTo(r.left,r.bottom);
- LineTo(r.right,r.bottom);
- LineTo(r.right,r.top);
- LineTo(er.right+gap,r.top);
-
- PenNormal();
- }
-
- Boolean Split(const StringPtr sub,const StringPtr s,StringPtr s1,StringPtr s2){
- short p;
-
- p=TPPos(sub,s);
- if (p>0){
- TPCopy(s1,s,1,p-1);
- TPCopy(s2,s,p+sub[0],255);
- }
-
- return (p>0);
- }
-
- short StrToNum(const StringPtr s){
- long n;
-
- StringToNum(s,&n);
-
- return LoWord(n);
- }
-
- /*
- DisplayStyledString
-
- Displays a string with embedded style characteristics.
-
- Styled strings are in the form of:
-
- "\pFONT:SIZE:STYLE:JUST:TEXT"
-
- where font is the font number,
- size is the size in pixels,
- style is the correct Style, (h==hot, display URL's underlined in blue)
- just is the justification mode,
- and text is the rest of the string to display.
- */
- void DisplayStyledString(DialogPtr dlg,short item,const StringPtr str){
- Rect box;
- short just,font,size,i,j,def_font,def_size,oldfont,oldsize;
- Str255 this,tmp,s;
- Style st,oldface;
- FontInfo fi;
- Boolean fixsize,hot; // parse for <> and blue-underline them
- TEHandle teh;
- TextStyle tsr;
-
- SetPort(dlg);
- oldfont=dlg->txFont;
- oldsize=dlg->txSize;
- oldface=dlg->txFace;
-
- def_font=geneva;
- def_size=9;
-
- GetDItemRect(dlg,item,&box);
-
- // make a local copy
- SetPString(s,1,str);
-
- // parse out the font: part
- if (Split("\p:",s,this,tmp)){
- hot=fixsize=false;
- SetPString(s,1,tmp);
-
- if (this[0]==0)
- font=def_font;
- else {
- GetFNum(this,&font);
- if (font==0){
- fixsize=true;
- font=def_font;
- }
- }
-
- if (Split("\p:",s,this,tmp)){
- SetPString(s,1,tmp);
-
- if (this[0]==0)
- size=def_size;
- else
- size=StrToNum(this);
-
- if (Split("\p:",s,this,tmp)){
- SetPString(s,1,tmp);
- st=(Style)0;
- for (i=1;i<=this[0];i++){
- if ((this[i]>='0')&&(this[i]<='7'))
- st+=(this[i]-'0');
- else if ((this[i]=='h')||(this[i]=='H'))
- hot=true;
- }
- if (Split("\p:",s,this,tmp)){
- SetPString(s,1,tmp);
- if (this[0]==0)
- just=teJustLeft;
- else
- just=StrToNum(this);
-
- TextFont(font);
- TextSize(size);
- TextFace(st);
-
- if (fixsize){
- GetFontInfo(&fi);
- while (fi.ascent+fi.descent>box.bottom-box.top){
- if (size>48)
- size=48;
- else if (size>36)
- size=36;
- else if (size>27)
- size=27;
- else if (size>24)
- size=24;
- else if (size>18)
- size=18;
- else if (size>14)
- size=14;
- else if (size>12)
- size=12;
- else {
- size=9;
- TextSize(size);
- break;
- }
- TextSize(size);
- GetFontInfo(&fi);
- }
- }
- if (false)
- TETextBox(&(s[1]),s[0],&box,just);
- else {
- // TEStyleNew box (perhaps for handling larger windows?
- teh=TEStyleNew(&box,&box);
- if (teh!=(TEHandle)0){
- TESetText(&(s[1]),s[0],teh);
- TESetAlignment(just,teh);
- if (hot){
- for (i=1;i<=s[0];i++){
- if (s[i]=='<'){
- j=i+1;
- while ((j<=s[0])&&(s[j]!='>'))
- j++;
- TESetSelect(i,j-1,teh);
- i=j;
- tsr.tsFace=st+underline;
- tsr.tsColor.red=tsr.tsColor.green=0;
- tsr.tsColor.blue=0xffff;
- TESetStyle(doFace+doColor,&tsr,false,teh);
- }
- }
- }
-
- TEUpdate(&box,teh);
- TEDispose(teh);
- }
- }
- }
-
- TextFont(oldfont);
- TextSize(oldsize);
- TextFace(oldface);
- }
- }
- }
- }
-
- void AddTrackItem(WindowPtr window,RgnHandle rgn,short i){
- Rect itemrect;
- RgnHandle tmp;
-
- if (i){
- GetDItemRect(window,i,&itemrect);
- tmp=NewRgn();
- RectRgn(tmp,&itemrect);
- UnionRgn(rgn,tmp,rgn);
- DisposeRgn(tmp);
- }
- }
-
- Boolean TrackItems(WindowPtr window,short i1,short i2,short i3){
- RgnHandle rgn;
- Boolean inside=true,newinside;
- Point mouse;
-
- SetPort(window);
- rgn=NewRgn();
- AddTrackItem(window,rgn,i1);
- AddTrackItem(window,rgn,i2);
- AddTrackItem(window,rgn,i3);
-
- InvertRgn(rgn);
-
- while (StillDown()){
- GetMouse(&mouse);
- newinside=PtInRgn(mouse,rgn);
- if (newinside!=inside){
- InvertRgn(rgn);
- inside=newinside;
- }
- }
-
- if (inside)
- InvertRgn(rgn);
-
- DisposeRgn(rgn);
-
- return inside;
- }
-
-
-